/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	/// <summary>
	/// Provides a stream inteface for Reading and Writing to GPU buffer data.
	/// </summary>
	public ref class BufferStream : public System::IO::Stream
	{
	public:

		/// <summary>
		/// Creates a new BufferStream and copies the data provided by the source Stream. 
		/// This BufferStream cannot be written to after it has been created.<br/>
		/// This will automatically close the source stream.
		/// </summary>
		BufferStream( System::IO::Stream^ source );
	internal:
		BufferStream( void* buffer, System::Int64 sizeInBytes, bool canRead, bool canWrite, bool ownData );

	protected:
		BufferStream() { }

		char* mBuffer;
		bool mCanRead;
		bool mCanWrite;
		bool mOwnData;

		System::Int64 mSize;
		System::Int64 mPosition;

	public:

		/// <summary>
		/// Releases managed resources
		/// </summary>
		virtual ~BufferStream() { }

		/// <summary>
		/// Releases unmanaged resources
		/// </summary>
		!BufferStream();

		/// <summary>
		/// Reads an element from the stream, and advances the current position within this stream by the number of elements read.
		/// </summary>
		/// <param name="data">Element in which to copy into the stream.</param>
		generic<typename T> where T : value class
		T Read();

		/// <summary>
		/// Reads an element from the stream, and advances the current position within this stream by the number of elements read.
		/// </summary>
		/// <param name="data">Element in which to copy into the stream.</param>
		generic<typename T> where T : value class
		void Read( T% data );

		/// <summary>
		/// Reads a sequence of bytes from the stream and advances the position within the stream by the number of bytes read.
		/// </summary>
		/// <param name="buffer">Buffer to hold the data read from the stream.</param>
		/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes from the stream.</param>
		/// <param name="numBytes">The number of bytes to read from the stream.</param>
		/// <returns>The number of bytes read from the stream.</returns>
		virtual int Read( array<System::Byte>^ buffer, int offset, int numBytes ) override
		{
			return ReadRange<System::Byte>( buffer, offset, numBytes );
		}

		/// <summary>
		/// Reads a sequence of elements from the stream and advances the position within the stream by the number of elements read.
		/// </summary>
		/// <param name="buffer">Buffer to hold the data read from the stream.</param>
		/// <returns>The number of bytes read from the stream.</returns>
		generic<typename T> where T : value class
		int ReadRange( array<T>^ buffer )
		{
			return ReadRange<T>( buffer, 0, buffer->Length );
		}

		/// <summary>
		/// Reads a sequence of elements from the stream and advances the position within the stream by the number of elements read.
		/// </summary>
		/// <param name="buffer">Buffer to hold the data read from the stream.</param>
		/// <param name="offset">The zero-based offset in buffer at which to begin storing the data read from the current stream.</param>
		/// <param name="numElems">The number of elements to be read from the stream.</param>
		/// <returns>The number of bytes read from the stream.</returns>
		generic<typename T> where T : value class
		int ReadRange( array<T>^ buffer, int offset, int numElems );

		/// <summary>
		/// Writes a sequence of bytes to the stream and advances the current position within this stream by the number of bytes written.
		/// </summary>
		/// <param name="buffer">Buffer in which to copy into the stream.</param>
		/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the stream.</param>
		/// <param name="numBytes">The number of bytes to be written to the current stream.</param>
		virtual void Write( array<System::Byte>^ buffer, int offset, int numBytes ) override
		{
			WriteRange<System::Byte>( buffer, offset, numBytes );
		}

		/// <summary>
		/// Writes an element to the stream, and advances the current position within this stream by the number of elements written.
		/// </summary>
		/// <param name="data">Element in which to copy into the stream.</param>
		generic<typename T> where T : value class
		void Write( T data );

		/// <summary>
		/// Writes a sequence of elements to the stream, and advances the current position within this stream by the number of elements written.
		/// </summary>
		/// <param name="buffer">Buffer in which to copy into the stream.</param>
		generic<typename T> where T : value class
		void WriteRange( array<T>^ buffer )
		{
			WriteRange<T>( buffer, 0, buffer->Length );
		}

		/// <summary>
		/// Writes a sequence of elements to the stream, and advances the current position within this stream by the number of elements written.
		/// </summary>
		/// <param name="buffer">Buffer in which to copy into the stream.</param>
		/// <param name="offset">The zero-based offset in buffer at which to begin copying values to the stream.</param>
		/// <param name="numElems">The number of elements to be written to the stream.</param>
		generic<typename T> where T : value class
		void WriteRange( array<T>^ buffer, int offset, int numElems );		
		
		/// <summary>Sets the position within the current stream.</summary>
		/// <exception cref="InvalidOperationException">Attempted to seek outside of the bounds of the stream.</exception>
		virtual System::Int64 Seek( System::Int64 offset, System::IO::SeekOrigin origin ) override;

		/// <summary>
		/// Saves the stream to the specfied file.
		/// </summary>
		// <param name="path">Path of the file that the data will be written to. If 'path' does not exist it will be created.</param>
		/// <returns>True if succesful, False otherwise.</returns>
		bool Save( System::String^ path );

		/// <summary>
		/// Not supported.
		/// </summary>
		virtual void Flush() override { }

		/// <summary>
		/// Not supported.
		/// </summary>
		virtual void SetLength( System::Int64 value ) override { }

		/// <summary>
		/// Gets a value indicating whether the current stream supports reading.
		/// </summary>
		property bool CanRead
		{
			virtual bool get() override { return mCanRead; }
		}

		/// <summary>
		/// Gets a value indicating whether the current stream supports seeking.
		/// </summary>
		property bool CanSeek
		{
			virtual bool get() override { return true; }
		}

		/// <summary>
		/// Gets a value indicating whether the current stream supports writing.
		/// </summary>
		property bool CanWrite
		{
			virtual bool get() override { return mCanWrite; }
		}

		/// <summary>
		/// Get the length in bytes of the stream.
		/// </summary>
		property System::Int64 Length
		{
			virtual System::Int64 get() override { return mSize; }
		}

		/// <summary>
		/// Gets or sets the position within the current stream.
		/// </summary>
		property System::Int64 Position
		{
			virtual System::Int64 get() override { return mPosition; }
			virtual void set( System::Int64 pos ) override { mPosition = pos; }
		}

		/// <summary>
		/// Gets a pointer to the underlying data
		/// </summary>
		property System::IntPtr DataPointer
		{
			System::IntPtr get() { return System::IntPtr( mBuffer ); }
		}
	};
}